﻿using System;

namespace Xfs
{
    public class XfsOuterMessageDispatcher : IXfsMessageDispatcher
    {
        public void Dispatch(XfsSession session, ushort opcode, object message)
        {
            this.DispatchAsync(session, opcode, message).Coroutine();
        }

        public async XfsVoid DispatchAsync(XfsSession session, ushort opcode, object message)
        {
            /// 根据消息接口判断是不是Actor消息，不同的接口做不同的处理
            switch (message)
            {
                #region
                //case IXfsActorLocationRequest actorLocationRequest: /// gate session收到actor rpc消息，先向actor 发送rpc请求，再将请求结果返回客户端
                //    {

                //        //long unitId = session.GetComponent<XfsSessionPlayerComponent>().Player.UnitId;
                //        //XfsActorLocationSender actorLocationSender = XfsGame.Scene.GetComponent<ActorLocationSenderComponent>().Get(unitId);
                //        //int rpcId = actorLocationRequest.RpcId; // 这里要保存客户端的rpcId
                //        //long instanceId = session.InstanceId;
                //        //IXfsResponse response = await actorLocationSender.Call(actorLocationRequest);
                //        //response.RpcId = rpcId;

                //        //// session可能已经断开了，所以这里需要判断
                //        //if (session.InstanceId == instanceId)
                //        //{
                //        //    session.Reply(response);
                //        //}

                //        break;
                //    }
                //case IXfsActorLocationMessage actorLocationMessage: /// gate session收到actor rpc消息，向actor 发送请求
                //    {

                //        //long unitId = session.GetComponent<SessionPlayerComponent>().Player.UnitId;
                //        //ActorLocationSender actorLocationSender = Game.Scene.GetComponent<ActorLocationSenderComponent>().Get(unitId);
                //        //actorLocationSender.Send(actorLocationMessage);

                //        break;
                //    }
                #endregion

                case IXfsActorRequest actorRequest:  /// 分发IXfsActorRequest消息
                    {
                        XfsEntity entity = (XfsEntity)XfsGame.EventSystem.Get(actorRequest.ActorId);

                        if (entity == null)
                        {
                            Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + $" 53. not found actor: {message}");
                            XfsActorResponse response = new XfsActorResponse
                            {
                                Error = XfsErrorCode.ERR_NotFoundActor,
                                RpcId = actorRequest.RpcId
                            };
                            session.Reply(response);
                            return;
                        }

                        XfsMailboxComponent mailBoxComponent = entity.GetComponent<XfsMailboxComponent>();
                        if (mailBoxComponent == null)
                        {
                            XfsActorResponse response = new XfsActorResponse
                            {
                                Error = XfsErrorCode.ERR_ActorNoMailBoxComponent,
                                RpcId = actorRequest.RpcId
                            };
                            session.Reply(response);
                            Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType().Name + $" 75. not found actor: {message}");
                            return;
                        }

                        mailBoxComponent.Add(new XfsActorMessageInfo() { Session = session, Message = actorRequest });
                        break;
                    }
                case IXfsActorMessage iactorMessage:  /// 分发IXfsActorMessage消息
                    {
                        XfsEntity entity = (XfsEntity)XfsGame.EventSystem.Get(iactorMessage.ActorId);

                        if (entity == null)
                        {
                            Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType() + $"not found actor: {message}");
                            return;
                        }

                        XfsMailboxComponent mailBoxComponent = entity.GetComponent<XfsMailboxComponent>();
                        if (mailBoxComponent == null)
                        {
                            Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType() + $"actor not add MailBoxComponent: {entity.GetType().Name} {message}");
                            return;
                        }

                        mailBoxComponent.Add(new XfsActorMessageInfo() { Session = session, Message = iactorMessage });
                        break;
                    }
                case IXfsRequest xfsRequest:  /// 分发IXfsRequest消息
                    {
                        XfsGame.XfsSence.GetComponent<XfsMessageDispatcherComponent>().Handle(session, new XfsMessageInfo() { Opcode = opcode, Message = message });

                        break;
                    }
                case IXfsMessage iMessage:  /// 分发IXfsMessage消息
                    {
                        XfsGame.XfsSence.GetComponent<XfsMessageDispatcherComponent>().Handle(session, new XfsMessageInfo() { Opcode = opcode, Message = message });

                        break;
                    }
                default:
                    {
                        /// 分发其他消息（无）

                        Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + this.GetType() + " 181. 分发其他消息（无）: " + message.GetType().Name);
                        break;
                    }
            }

        }


    }
}